import 'package:flutter/material.dart';
import 'package:flutter_div/div_col.dart';
import 'package:flutter_div/div_list.dart';
import 'package:flutter_div/div_row.dart';
import 'package:flutter_div/div_style.dart';

class Div extends StatelessWidget{

  /// Div布局
  const Div({
    super.key,
    this.style,
    this.styles,
    this.child,
    this.onTap,
  });

  final void Function()? onTap;
  final DivStyle? style;
  final List<DivStyle>? styles;
  final Widget? child;

  @override
  Widget build(BuildContext context) {

    var _style = DivStyle();
    styles?.forEach((e){
      _style = _style.merge(e);
    });
    _style = _style.merge(style);


    var container = Container(
      key: key,
      alignment: _style.align,
      padding: _style.padding,
      margin: _style.margin,
      width: _style.width,
      height: _style.height,
      decoration: BoxDecoration(
        color: _style.color,
        border: _style.border,
        borderRadius: _style.radius,
        image: _style.image,
        boxShadow: _style.boxShadow,
        gradient: _style.gradient,
      ),
      clipBehavior: _style.clip,
      child: child,
    );

    if(onTap!=null){
      return GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: onTap,
        child: container,
      );
    }

    return container;
  }

  /// Div横轴布局
  static Widget row(List<Widget> children, {
    Key? key,
    DivStyle? style,
    List<DivStyle>? styles,
    void Function()? onTap,
  }){

    return DivRow(
      key: key,
      children: children,
      style: style,
      styles: styles,
      onTap: onTap,
    );
  }
  
  /// Div、纵轴布局
  static Widget col(List<Widget> children, {
    Key? key,
    DivStyle? style,
    List<DivStyle>? styles,
    void Function()? onTap,
  }){

    return DivCol(
      children: children,
      key: key,
      style: style,
      styles: styles,
      onTap: onTap,
    );
  }
  
  /// Div横轴分割线
  static Widget xline({
    Key? key,
    Color color = Colors.black,
    double height = 1,
    double thick = 0,
  }){
    return Div(
      key: key,
      style: DivStyle(
        color: color,
        height: height,
        margin: EdgeInsets.only(top: thick, bottom: thick),
      ),
    );
  }
  
  /// Div纵轴分割线
  static Widget yline({
    Key? key,
    Color color = Colors.black,
    double width = 1,
    double? height,
    double thick = 0,
  }){
    return Div(
      key: key,
      style: DivStyle(
        color: color,
        width: width,
        height: height,
        margin: EdgeInsets.only(left: thick, right: thick),
      ),
    );
  }
  
  /// Div列表
  static Widget list<T>({
    Key? key,
    required Future<bool> Function(int page, List<T> items, bool refresh) onLoad,
    required Widget Function(BuildContext context, T item, int index) itemBuilder,
    Widget Function(BuildContext context, List<T> items)? headerBuilder,
    Widget Function(BuildContext context, List<T> items)? footerBuilder,
    DivListData<T>? data,
    DivStyle? style,
    List<DivStyle>? styles,
  }){
    return Div(
      key: key,
      child: DivList(
        key: key,
        onLoad: onLoad,
        itemBuilder: itemBuilder,
        data:data,
      ),
      style: style,
      styles: styles,
    );
  }
}
